package com.cn.kehong.service;

import com.cn.kehong.api.*;
import com.cn.kehong.dao.DataSubmissionMapper;
import com.cn.kehong.domain.*;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.Constants;
import com.cn.kehong.utils.QueryForm;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DataSubmissionServiceImpl implements IDataSubmissionService {

    @Autowired
    private DataSubmissionMapper dataSubmissionMapper;

    @Autowired
    private IDataSubmissionCcService dataSubmissionCcService;

    @Autowired
    private IDataSubmissionScService dataSubmissionScService;

    @Autowired
    private IDataSubmissionYyService dataSubmissionYyService;
    @Autowired
    private IDataSubmissionKbService dataSubmissionKbService;
    @Override
    public List<DataSubmission> getDataSubmissionList(QueryForm queryForm) throws Exception {
        int count = dataSubmissionMapper.selectDataSubmissionListCount(queryForm);
        if (count == 0) {
            return Lists.newArrayList();
        }
        List<DataSubmission> dataSubmissions = dataSubmissionMapper.selectDataSubmissionList(queryForm);
        dataSubmissions.get(0).setCount(count);
        return dataSubmissions;
    }

    @Override
    public String addDataSubmission(DataSubmission dataSubmission) throws Exception {
        String fillTime = dataSubmission.getDataFillTime();
        String dataType = dataSubmission.getDataType();
        if(StringUtils.isEmpty(fillTime)||StringUtils.isEmpty(dataType)){
            return "参数错误!";
        }
        DataSubmissionExample example = new DataSubmissionExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andDataFillTimeEqualTo(fillTime).andDataTypeEqualTo(dataType);
        int existCount = dataSubmissionMapper.countByExample(example);
        if(existCount > 0){
            return "当日计划已经存在！";
        }
        String id = CommonUtil.generateRandomNum("submission-");
        dataSubmission.setId(id);
        if(StringUtils.equals("1",dataType)||StringUtils.equals("10",dataType)||StringUtils.equals("11",dataType)||StringUtils.equals("12",dataType)||StringUtils.equals("13",dataType)){
            List<DataSubmissionYy> dataSubmissionYys = dataSubmission.getDataSubmissionYys();
            if(CollectionUtils.isEmpty(dataSubmissionYys)){
                return "参数异常，dataSubmissionYys is 0！";
            }
            for(DataSubmissionYy data:dataSubmissionYys){
                data.setSubmissionId(id);
            }
            String resp  = dataSubmissionYyService.addDataSubmissionYy(dataSubmissionYys);
            if(StringUtils.isNotEmpty(resp)){
                return resp;
            }
        }else if(StringUtils.equals("2",dataType)){
            List<DataSubmissionSc> dataSubmissionScs = dataSubmission.getDataSubmissionScs();
            if(CollectionUtils.isEmpty(dataSubmissionScs)){
                return "参数异常，dataSubmissionScs is 0！";
            }
            for(DataSubmissionSc data:dataSubmissionScs){
                data.setSubmissionId(id);
            }
            String resp  = dataSubmissionScService.addDataSubmissionSc(dataSubmissionScs);
            if(StringUtils.isNotEmpty(resp)){
                return resp;
            }
        }else{
            List<DataSubmissionCc> dataSubmissionCcs = dataSubmission.getDataSubmissionCcs();
            if(CollectionUtils.isEmpty(dataSubmissionCcs)){
                return "参数异常,dataSubmissionCcs is 0！";
            }
            for(DataSubmissionCc data:dataSubmissionCcs){
                data.setSubmissionId(id);
            }
            String resp  = dataSubmissionCcService.addDataSubmissionCc(dataSubmissionCcs);
            if(StringUtils.isNotEmpty(resp)){
                return resp;
            }
        }

        int count = dataSubmissionMapper.insertSelective(dataSubmission);
        if(count < 1){
            throw new Exception("插入数据异常");
        }
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateDataSubmission(DataSubmission dataSubmission) throws Exception {
        String id = dataSubmission.getId();
        if(StringUtils.isEmpty(id)){
            return "缺少主键";
        }
        String dataType = dataSubmission.getDataType();

        if(StringUtils.equals("1",dataType)||StringUtils.equals("10",dataType)||StringUtils.equals("11",dataType)||StringUtils.equals("12",dataType)||StringUtils.equals("13",dataType)){
            List<DataSubmissionYy> dataSubmissionYys = dataSubmission.getDataSubmissionYys();
            if(CollectionUtils.isEmpty(dataSubmissionYys)){
                return "参数异常！";
            }
            String resp  = dataSubmissionYyService.updateDataSubmissionYy(dataSubmissionYys);
            if(StringUtils.isNotEmpty(resp)){
                return resp;
            }
        }else if(StringUtils.equals("2",dataType)){
            List<DataSubmissionSc> dataSubmissionScs = dataSubmission.getDataSubmissionScs();
            if(CollectionUtils.isEmpty(dataSubmissionScs)){
                return "参数异常！";
            }
            String resp  = dataSubmissionScService.updateDataSubmissionSc(dataSubmissionScs);
            if(StringUtils.isNotEmpty(resp)){
                return resp;
            }
        }else if(StringUtils.equals("4",dataType)){
            List<DataSubmissionCc> dataSubmissionCcs = dataSubmission.getDataSubmissionCcs();
            if(CollectionUtils.isEmpty(dataSubmissionCcs)){
                return "参数异常！";
            }
            String resp  = dataSubmissionCcService.updateDataSubmissionCc(dataSubmissionCcs);
            if(StringUtils.isNotEmpty(resp)){
                return resp;
            }
        }

        DataSubmissionExample example = new DataSubmissionExample();
        example.createCriteria().andIdEqualTo(id);

        int count = dataSubmissionMapper.updateByExampleSelective(dataSubmission,example);
        if(count < 1){
            throw new Exception("更新数据异常");
        }
        return CommonUtil.outStr(count);
    }

    @Override
    public String deleteDataSubmissionById(String id) throws Exception {
        if(StringUtils.isEmpty(id)){
            return "主键不能为空";
        }
        DataSubmission update = new DataSubmission();
        update.setStatus(Constants.DEL_STATUS);
        DataSubmissionExample example = new DataSubmissionExample();
        example.createCriteria().andIdEqualTo(id);
        int count = dataSubmissionMapper.updateByExampleSelective(update,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public String addDataSubmissionByKb(DataSubmission dataSubmission) throws Exception {
        String fillTime = dataSubmission.getDataFillTime();
        String dataType = dataSubmission.getDataType();
        if(StringUtils.isEmpty(fillTime)||StringUtils.isEmpty(dataType)){
            return "参数错误!";
        }
        DataSubmissionExample example = new DataSubmissionExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andDataFillTimeEqualTo(fillTime).andDataTypeEqualTo(dataType);
        int existCount = dataSubmissionMapper.countByExample(example);
        if(existCount > 0){
            return "当日计划已经存在！";
        }
        String id = CommonUtil.generateRandomNum("submission-");
        dataSubmission.setId(id);
         List<DataSubmissionKb> dataSubmissionKbs = dataSubmission.getDataSubmissionKbs();
            if(CollectionUtils.isEmpty(dataSubmissionKbs)){
                return "参数异常，dataSubmissionKbs is 0！";
            }
            for(DataSubmissionKb data:dataSubmissionKbs){
                data.setId(CommonUtil.generateRandomNum("submissionKb-"));
                data.setSubmissionId(id);
                data.setStatus("0");
                data.setStoreType(dataSubmission.getDataType());
            }
            String resp  = dataSubmissionKbService.addDataSubmissionKb(dataSubmissionKbs);
            if(StringUtils.isNotEmpty(resp)){
                return resp;
            }

        int count = dataSubmissionMapper.insertSelective(dataSubmission);
        if(count < 1){
            throw new Exception("插入数据异常");
        }
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateDataSubmissionByKb(DataSubmission dataSubmission) throws Exception {
        String id = dataSubmission.getId();
        if(StringUtils.isEmpty(id)){
            return "缺少主键";
        }
            List<DataSubmissionKb> dataSubmissionKbs = dataSubmission.getDataSubmissionKbs();
            if(CollectionUtils.isEmpty(dataSubmissionKbs)){
                return "参数异常！";
            }
                String resp  = dataSubmissionKbService.updateDataSubmissionKb(dataSubmissionKbs);
                if(StringUtils.isNotEmpty(resp)){
                    return resp;
                }

        DataSubmissionExample example = new DataSubmissionExample();
        example.createCriteria().andIdEqualTo(id);

        int count = dataSubmissionMapper.updateByExampleSelective(dataSubmission,example);
        if(count < 1){
            throw new Exception("更新数据异常");
        }
        return CommonUtil.outStr(count);
    }
}
